import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:star_author/provider/utils/persist.dart';
import 'package:star_author/provider/utils/provider_lib.dart';

class UserProvider with ChangeNotifier, ProviderLib, Persist {
  Map<String, dynamic> _data = {
    "token": null,
    'userInfo': {'id': 0},
  };

  int count = 0;

  UserProvider() {
    _data = persistInit(key: 'user', defaultData: _data);
    providerInit(
      _data,
      notify: notifyListeners,
      persistSave: persistSave,
    );
    notifyListeners();
  }

  String? get token => _data['token'];

  Map<dynamic, dynamic>? get user => _data['userInfo'] ?? {'id': 0};

  bool get isLogin => token != null;

  bool get isBidder {
    return (user?['is_bidder'] ?? false) || (user?['is_vip'] ?? false);
  }

  bool get isAnchor => (user?['maintain_studios'] ?? []).length > 0;

  //没有实名--需要实名
  get realNameAuthentication {
    return !(user?['real_name_authentication'] ?? false);
  }

  //没有绑定银行卡 --去绑定银行卡
  get toBindBankCard {
    return (user?['adapay_settle_account_id'] ?? "").toString().isEmpty;
  }

  //激活汇付帐号
  get activationBank {
    return (user?['adapay_member_id'] ?? "").toString().isEmpty;
  }

  //银行卡尾号
  get bankLastDigits {
    if (user?['identification']['account_no'].length == 0) return "";
    return user?['identification']['account_no'].substring(
        user?['identification']['account_no'].length - 4,
        user?['identification']['account_no'].length);
  }

  void clearToken() {
    set(key: 'token', value: null);
    set(key: 'userInfo', value: {'id': 0});
    notifyListeners();
  }

  bool get isWhite {
    return user?['is_vip'] ?? false;
  }
}
